package com.demo.controller;

import com.demo.function.shiro.service.ShiroService;
import com.demo.model.entity.User;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * shiro控制器
 */
@Slf4j
@RestController
@RequestMapping("/shiro")
public class ShiroController {

    @Resource
    private ShiroService shiroService;

    @GetMapping("/login")
    public Long login(@RequestParam(name = "username") String username) {
        User user = shiroService.login(username);
        return user.getId();
    }

    /**
     * 退出，销毁shiro session
     */
    @GetMapping("/logout")
    public void logout() {
        SecurityUtils.getSubject().logout();
    }

    /**
     * shiro admin权限校验测试
     * @return 消息
     */
    @GetMapping("/getShiroAdminMsg")
    @RequiresPermissions("admin")
    public String getAuthAdminMsg() {
        log.info("shiro admin权限校验测试");
        return "shiro admin权限校验测试，时间：" + new Date();
    }

    /**
     * shiro user权限校验测试
     * @return 消息
     */
    @GetMapping("/getShiroUserMsg")
    @RequiresPermissions("user")
    public String getAuthUserMsg() {
        log.info("shiro user权限校验测试");
        return "shiro user权限校验测试，时间：" + new Date();
    }

    /**
     * shiro校验权限测试，只要登录成功所有用户都可以访问
     * @return 消息
     */
    @GetMapping("/getShiroMsg")
    public String getAuthMsg() {
        log.info("shiro校验权限测试");
        return "shiro校验权限测试，时间：" + new Date();
    }

    /**
     * shiro不校验权限测试
     * @return 消息
     */
    @GetMapping("/getMsg")
    public String getMsg() {
        log.info("shiro不校验权限测试");
        return "shiro不校验权限测试，时间：" + new Date();
    }
}
